package first.forHuaWei.codeTop;

/**
 * @ClassName LC111
 * @description:
 * @author: 荣燊
 * @create: 2021-09-13 10:51
 **/
public class LC111 {
    // 二叉树的最小深度
    public static int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        // 如果左右孩子有一个为空，返回的是最大的那个孩子层数。
        int m1 = minDepth(root.left);
        int m2 = minDepth(root.right);
        if (root.left == null || root.right == null) {
            return m1 + m2 + 1;
        }
        return Math.min(m1, m2)+1;
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {}

        TreeNode(int val) { this.val = val; }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
